<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/17
 * Time: 上午8:55
 */

namespace app\doctor\controller;

use think\Db;
use think\Request;

/**
 * 记录列表
 *
 */
class Record extends  Basecontroller
{
    protected $noNeedLogin = [];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('patientcase');

    }
    /**
     * 记录列表
     */
    public function index()
    {

        //搜索
        $request = Request::instance();
     $currentDate_s_e = getthemonth(date("Y-m-d"));
        $pageQuery = [];
        $option['patient_case.is_deleted'] = ['=', 1];
       // $option['doctor_id'] = $this->doctor->id;

        //默认年月日
      //  $pageQuery['query']['start'] = $currentDate_s_e[0];

      //  $pageQuery['query']['end'] = $currentDate_s_e[1];

        if ($request->isGet()) {
            $data = $request->param();

            if (isset($data['name']) && trim($data['name'])) {
                $option['patient.name|patient_case.case_num'] = array('like', '%' . trim($data['name']) . '%');
                $pageQuery['query']['name'] = trim($data['name']);

            }
            if (isset($data['start']) && $data['start'] !== '') {

                $last_date =  date('Y-m-d',strtotime($data['start']));
                $now_date =  date('Y-m-d',time());
                $sencond_data = $data['end'] ? $data['end'] : $now_date;
                $end =  date('Y-m-d',strtotime($sencond_data.'+1 day'));

                $option['patient_case.check_time'] = array('between', ["{$last_date}", "{$end}"]);
                $pageQuery['query']['start'] = trim($data['start']);
                $pageQuery['query']['end'] = trim($data['end']);

            }

            if (isset($data['doctor']) && $data['doctor'] !== '') {
                $option['doctor.name'] = array('like', '%' . trim($data['doctor']) . '%');
                $pageQuery['query']['doctor'] = trim($data['doctor']);
            }

            if (isset($data['patient_case_type']) && $data['patient_case_type'] !== '') {
                $option['patient_case.patient_case_type'] = $data['patient_case_type'];
                $pageQuery['query']['patient_case_type'] = trim($data['patient_case_type']);
            }
        }
        $this->assign('search', isset($pageQuery['query']) ? $pageQuery['query'] : '');
      //  print_r($option);
        $list = $this->model
            ->field('patient_case.*,patient.name as patient_name,patient.sex,patient.document_num,patient.birth,doctor.name as doctor_name,doctor_department.department as department_name')
            ->join('patient','patient.id=patient_case.patient_id')
            ->join('doctor','doctor.id=patient_case.doctor_id')
            ->join('doctor_department','doctor_department.id=doctor.depart_id')
            ->where($option)
            ->order('create_time desc')
            ->paginate(false, false, $pageQuery)
            ->each(function ($item, $key) {
                $item['patient_part_lat'] = json_decode($item['patient_part_lat'],true);
                $patient_case_type = Db::name('patient_case_type')->field('name')->where("id={$item['patient_case_type']}")->find();
                $item['patient_case_type'] = $patient_case_type['name'];
                return $item;
            });
        //print_r($list);
       // print_r(Db::name('patient_case')->getLastSql());
        $this->assign('list', $list);
            //获取检查类型
        $this->assign('url',$this->request->action());
        $options['is_deleted'] = 1;
        $options['type'] = 1;
        $case_select = $this->getCase_type($options);
        $this->assign("case_select",$case_select);

        return $this->fetch();

    }



    /**
     * 查看某位患者的记录
     */
    public function check_detail(){

        $cid = $this->request->param('id');
        //获取病人信息以及病例
        $this->getPatient_info_case($cid);
        //取出电子报告
        $this->getPatient_report($cid);
        //取出电子胶片
        $this->getPatient_film($cid);


        return $this->fetch();
    }

    /**
     * 添加记录
    */
    public function add_record(){



        //获取用户信息
        $data = $this->request->param();
        $patientInfo = $this->getPatient_ById($data['id']);
        $this->assign("patientInfo",$patientInfo);

        //获取检查类型
        $option['is_deleted'] = 1;
        $option['type'] = 1;
        $case_select = $this->getCase_type($option);
        $this->assign("case_select",$case_select);
        //获取维度/部位
        $option['is_deleted'] = 1;
        $option['type'] = 2;
        $case_depart = $this->getCase_type($option);
        $this->assign("case_depart",$case_depart);
        $option['is_deleted'] = 1;
        $option['type'] = 3;
        $case_lat = $this->getCase_type($option);
        $this->assign("case_lat",$case_lat);

        //获取当前的医生


        return $this->fetch();
    }


    /**
     *获取添加分组dcm图片的列表
    **/
    public function add_record_depart(){


        $num = $this->request->param('num');
        $num = $num+1;

        $html = $this->uploadDcmByJs($num);
        return $html;
    }


}
